package com.xsjl.user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsjl.common.entity.PageResult;
import com.xsjl.common.entity.Result;
import com.xsjl.common.entity.StatusCode;
import com.xsjl.common.utils.JwtUtil;
import com.xsjl.user.pojo.Admin;
import com.xsjl.user.service.AdminService;
import com.xsjl.user.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * admin控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;
	@Autowired
	private JwtUtil jwtUtil;
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/test")
	public Result test() {
		return new Result(true, StatusCode.OK, "sus");
	}

	@PostMapping(value = "/login")
	public Result login(@RequestBody Admin admin) {
		Admin adminLogin = adminService.adminLogin(admin.getLoginname(), admin.getPassword());
		if (adminLogin == null) {
			return new Result(false, StatusCode.LOGINERROR, "账号或密码输入错误,请重新输入!");
		}
		String token = jwtUtil.createJWT(adminLogin.getId(), adminLogin.getLoginname(), "admin");
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		map.put("name", admin.getLoginname());
		return new Result(true, StatusCode.OK, "登录成功", map);
	}
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true,StatusCode.OK,"查询成功",adminService.findAll());
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",adminService.findById(id));
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/search/{page}/{size}",method= RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Admin> pageList = adminService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Admin>(pageList.getTotal(), pageList.getRecords()) );
	}

	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch( @RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",adminService.findSearch(searchMap));
    }
	
	/**
	 * 增加
	 * @param admin
	 */
	@RequestMapping(method= RequestMethod.POST)
	public Result add(@RequestBody Admin admin  ){
		adminService.add(admin);
		return new Result(true,StatusCode.OK,"增加成功");
	}
	
	/**
	 * 修改
	 * @param admin
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Admin admin, @PathVariable String id ){
		admin.setId(id);
		adminService.update(admin);
		return new Result(true,StatusCode.OK,"修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result remove(@PathVariable String id, HttpServletRequest request){
		Claims claims = (Claims) request.getAttribute("admin_claims");
		if (claims == null) {
			return new Result(false, StatusCode.ACCESSERROR, "权限不足");
		}
		userService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}
	
}
